
package com.mt.components.general {
	
	import mx.collections.ArrayCollection;
	import mx.collections.HierarchicalData;
	
	/**
	 * 类名称：DataBox<br> 
	 * 类描述：带有父子管理的集合，该集合中只能存放ENode对象 主要用在属性结构上，<br>例如表格树，树等UI上的数据集合<br>
	 * 创建人：马腾<br>
	 * 创建时间：Nov 20, 2012 10:23:37 PM<br>
	 * 修改人：马腾<br>
	 * 修改时间：Nov 20, 2012 10:23:37 PM<br>
	 */
	public class DataBox extends HierarchicalData {
		
		[Bindable]private var resource:ArrayCollection = new ArrayCollection();		
		[Bindable]private var _allElements:ArrayCollection = new ArrayCollection();
		
		public function DataBox(){
			super(resource);
		}
		
		public function addNode(value:Node):void{
			if(value.parent == null){
				resource.addItem(value);
			}else{
				var index:int = _allElements.getItemIndex(value.parent);
				if(index != -1){
					var parent:Node = _allElements.getItemAt(index) as Node;
					value.parent = parent;
					if(parent.children == null){
						parent.children = new ArrayCollection();
					}
					parent.children.addItem(value);
				}else{
					resource.addItem(value);
				}
			}
			_allElements.addItem(value);
		}
		
		public function addAllNodes(values:ArrayCollection):void{
			if(values == null){
				throw new Error("values is null");
			}
			for(var i:int=0;i<values.length;i++){
				var obj:Object = values.getItemAt(i);
				if(obj is Node){
					addNode(obj as Node);
				}
			}
		}
		
		public function clear():void{
			this.resource.removeAll();
			this._allElements.removeAll();
		}
		
		public function getSelectedNodes():ArrayCollection{
			var selecteds:ArrayCollection = new ArrayCollection();
			for(var i:int=0;i<_allElements.length;i++){
				var node:Node = _allElements.getItemAt(i) as Node;
				if(node.isSelected){
					selecteds.addItem(node);
				}
			}
			return selecteds;
		}
		
		public function get allElements():ArrayCollection {
			return _allElements;
		}
		
		public function get rootNodes():ArrayCollection {
			return resource;
		}
	}
}